<template>
  <div class="wrap">
    <div class="d-flex align-items-end" v-if="true">
      <el-checkbox  v-model="isAssembly"
                    :disabled="componenState"
                    @change="changeAssembly"
                    :label="$t('message.MassDistributionCheckTool.assembly')"/>
      <el-checkbox  v-model="isGrouping"
                    :disabled="componenState"
                    @change="computeMassAttributeList"
                    :label="$t('message.MassDistributionCheckTool.grouping')"/>
    </div>
    <div class="d-flex align-items-end" v-if="true">

    </div>
    <div v-if="!loaded">
      <div id="dataLoading" v-loading="!loaded" :element-loading-text="$t('message.MassDistributionCheckTool.loading') + loadedPercent + $t('message.MassDistributionCheckTool.loadedPercent')"
           :style="{width: '100%', height: 10 + 'px'}">
        <iframe v-show="loaded" id="dialogPluginIframe"
                :style="{width: '100%', height: 10 + 'px'}"></iframe>
      </div>
    </div>
	  <div v-if="isEmptyAssemblyTree" >
      <div class="center-text">
        <p>{{$t('message.MassDistributionCheckTool.isEmptyAssemblyTree')}}</p>
      </div>
    </div>
    <div v-if="params.massAttributeElements.length > 0 && massAttributeInfo" class="border massAttributeMessageBox" tabindex="-1">
      <mass-distribution-check-info-content v-if="!!massAttributeInfo"
                                            :assembly-tree-mass-attribute-info-list="resultAssemblyTreeMassAttributeInfoList"
                                            :is-mass-re-computed="isMassReComputed"
                                            :is-mass-computed="isMassComputed"
                                            :is-export-histogram="isExportHistogram"
      />
    </div>
    <div class="btn">
      <el-button type="primary"
                 size="small"
                 :disabled="componenState || componenStateCompute"
                 @click="computeStandardMass">{{ $t('message.MassDistributionCheckTool.computeStandardMass') }}
      </el-button>
      <el-button type="primary"
                 size="small"
                 :disabled="componenState"
                 @click="exportHistogram">{{ $t('message.MassDistributionCheckTool.export') }}
      </el-button>
      <el-button size="small"
                 @click="cancel">{{ $t('message.MassDistributionCheckTool.close') }}
      </el-button>
    </div>
  </div>
</template>

<script>
import {massDistributionCheckCommand} from "../../../Command/MassDistributionCheckCommand";
import MassDistributionCheckInfoContent from "./MassDistributionCheckInfoContent.vue";
import {massDistributionCheckToolManager} from "./MassDistributionCheckToolManager";
import DialogMixin from "../DialogMixin";
import {copyJSON} from "three/addons/libs/ecsy.module";

export default {
  mixins: [DialogMixin],
  name: "CmdMassDistributionCheckTool",
  components: {MassDistributionCheckInfoContent},
  data() {
    return {
      assemblyTree: null,//装配树
      currentAssemblyTreeList: [],
      assemblyName: '总装配',
      assemblyTreeFlag: 'assembly',//总装配(assembly)/点选节点(node)标志
      assemblyTreeMassAttributeInfoList: [],//质量属性列表
      resultAssemblyTreeMassAttributeInfoList: [],//质量属性列表
      massAttributeInfo: null,//质量属性列表
      massAttribute: {
        no: 0,
        name: '',
        protoName: '',
        count: 1,
        density: '',
        densityUnit: 'g/mm³',
        mass: '',
        massUnit: 'g',
        volume: '',
        volumeUnit: 'mm³',
        area: '',
        areaUnit: 'mm²',
        weight: 0,
        standardWeight: 0,
        standardMass: 0,
        deviationWeight: 0,
        deviationMass: 0,
        isMassCovered: false,
        baryCenter: {//重心
          unit: 'mm',
          x: 0,
          y: 0,
          z: 0,
        },
        axesAndMoments: {//惯性主轴和惯性主力矩，由重心决定
          unit: 'g * mm²',
          Lx: '',
          Ly: '',
          Lz: '',
          Px: 0,
          Py: 0,
          Pz: 0,
        },
        BT: {//惯性张量，由重心决定，并且对齐输出的坐标系
          unit: 'g * mm²',
          Lxx : 0,
          Lyx  : 0,
          Lzx  : 0,
          Lxy  : 0,
          Lyy : 0,
          Lzy : 0,
          Lxz : 0,
          Lyz : 0,
          Lzz: 0,
        },
        OT: {//惯性张量，由输出坐标系决定
          unit: 'g * mm²',
          Lxx : 0,
          Lyx  : 0,
          Lzx  : 0,
          Lxy  : 0,
          Lyy : 0,
          Lzy : 0,
          Lxz : 0,
          Lyz : 0,
          Lzz: 0,
        },
      },
      protoRefToInstanceMap: [],
      allProtoRefToInstanceMap: [],
      assemblyMass: 1,
      isComputed: false,
      isMassComputed: true,
      isExportHistogram: true,
      isMassReComputed: false,
      workspaceMode: '',
      isInit: false,
      isAssembly: true,
      isGrouping: true,
      componenState: true,
      componenStateCompute: false,
      isCovMass: massDistributionCheckToolManager.isCovMass,
      massValue: massDistributionCheckToolManager.massValue,
      params: {
        massAttributeElements: [],//元素列表
        allMassAttributeElements: [],//元素列表
      },
      loaded: false,
      loadedPercent: 0,
      isEmptyAssemblyTree: false,
    }
  },
  methods: {
    initAssemblyTree(){
      if (this.workspaceMode === 'PartDocument') {
        //do nothing
      } else if (this.workspaceMode === 'AssemblyDocument') {
        this.loadedPercent = 0;
        this.componenState = true;
        this.currentAssemblyTreeList = this.assemblyTree;
        this.traverseAssemblyTree(true);
        this.computeMassAttributeList();
        this.pickMode();
      }
    },
    findInstance(data, inInstanceId) {
      for (let i = 0; i < data.length; i++) {
        if (data[i].id === inInstanceId) {
          return data[i]; // 找到直接返回true
        } else {
          // 如果当前项是数组，递归查找
          let result = this.findInstance(data[i].children, inInstanceId);
          if(result) {
            return result;
          }
        }
      }
      return null; // 没有找到返回false
    },
    pickMode(){
      if (this.workspaceMode === 'PartDocument') {
        //do nothing
      } else if (this.workspaceMode === 'AssemblyDocument')
      {
        massDistributionCheckCommand.handlePickInstance((instances) => {
          console.log(instances);
          if(instances.length === 1) {
            this.params.massAttributeElements = [];
            this.protoRefToInstanceMap = [];
            this.loadedPercent = 0;
            this.componenState = true;
            this.isAssembly = true;
            this.componenStateCompute = false;
            //let subassembly = this.assemblyTree.find(item => item.id === instances[0].id);
            let subassembly = this.findInstance(this.assemblyTree, instances[0].id);
            let subassemblyList = [];
            if(subassembly) {
              this.assemblyName = subassembly.name;
              if (subassembly.children.length === 0) {
                subassemblyList.push(subassembly);
              } else {
                subassembly.children.forEach(item => {
                  subassemblyList.push(item);
                });
              }
            }
            this.currentAssemblyTreeList = subassemblyList;
            this.traverseAssemblyTree(false);
            this.computePartMassAttributeList();
          } else if(instances.length > 1) {
            let msg ='不支持多选实例';
            massDistributionCheckCommand.eventBusAlertMsg(msg, 'warning');
            return;
            this.params.massAttributeElements = [];
            this.protoRefToInstanceMap = [];
            this.assemblyName = '所选子件';
            let subassemblyList = [];
            instances.forEach(instance => {
              let subassembly = this.assemblyTree.find(item => item.id === instance.id);
              if(subassembly) {
                subassemblyList.push(subassembly);
              }
            });
            this.currentAssemblyTreeList = subassemblyList;
            this.traverseAssemblyTree(false);
          }
          //this.computePartMassAttributeList();
        });
      }
    },
    traverseAssemblyTree(isAll){
      if(this.currentAssemblyTreeList.length === 0 || !this.currentAssemblyTreeList ) {
        return;
      }
      this.currentAssemblyTreeList.forEach(instance => {
        let instanceIds = [];
        instanceIds.push(instance.id);
        let InstanceInfo = {
          name: instance.name,
          type: "",
          instanceId: instance.id,
        };
        let paramId = {
          instanceId: instance.id,
          elementId: instance.elementId
        }
        let isSameInstance = this.params.massAttributeElements.some((item) => instance.id === item.instanceId);
        if (isSameInstance) {
          massDistributionCheckCommand.clearHighLightInstanceElement(paramId);
          let instanceIds = this.params.massAttributeElements.map(e => e.instanceId);
          if (instanceIds) {
            massDistributionCheckCommand.highLightInstances(instanceIds);
          }
          return;
        }
        //按proto归类零件
        massDistributionCheckCommand.getInstanceProtoRefByInstanceId(instance.id,(data) => {
          let protoRefId = data.protoRefId;
          let protoRefDocId = data.protoRefDocId;
          let protoRefVersionId = data.protoRefVersionId;
          let protoRefName = data.protoRefName;
          if(protoRefName === "") {
            protoRefName = instance.name;
          }
          let protoRef = {
            protoRefId: protoRefId,
            protoRefDocId: protoRefDocId,
            protoRefVersionId: protoRefVersionId,
            protoRefName: protoRefName,
          };
          let instanceList = [];
          let index = instanceList.findIndex(item => item.instanceId === InstanceInfo.instanceId);
          if(instanceList.length === 0 && index === -1) {
            instanceList.push(InstanceInfo);
          }
          if(this.protoRefToInstanceMap.length === 0) {
            let item = {protoRef: protoRef, instanceList: instanceList};
            this.protoRefToInstanceMap.push(item);
          } else {
            let index = this.protoRefToInstanceMap.findIndex(item => item.protoRef.protoRefId === protoRefId);
            if(index !== -1) {
              let pIndex = this.protoRefToInstanceMap[index].instanceList.findIndex(ins => ins.instanceId === InstanceInfo.instanceId);
              if(pIndex === -1) {
                this.protoRefToInstanceMap[index].instanceList.push(InstanceInfo);
              }
            } else {
              let item = {protoRef: protoRef, instanceList: instanceList};
              this.protoRefToInstanceMap.push(item);
              if(isAll) {
                this.allProtoRefToInstanceMap = this.protoRefToInstanceMap;
              }
            }
          }
        });
        massDistributionCheckCommand.clearHighLightInstanceElement(paramId);
        this.params.massAttributeElements.push(InstanceInfo);
        if(isAll) {
          this.params.allMassAttributeElements = this.params.massAttributeElements;
        }
        //this.computeMassAttribute();
      });
      // this.currentAssemblyTreeList = [];
    },
    changeAssembly(value) {
      // if(this.isAssembly) {
      //   if(!(this.params.massAttributeElements === this.params.allMassAttributeElements && this.protoRefToInstanceMap === this.allProtoRefToInstanceMap)) {
      //     this.loadedPercent = 0;
      //     this.componenState = true;
      //     this.assemblyName = "总装配";
      //     this.params.massAttributeElements = this.params.allMassAttributeElements;
      //     this.protoRefToInstanceMap = this.allProtoRefToInstanceMap;
      //     this.computeMassAttributeList();
      //   }
      // }
      if(this.isAssembly) {
        this.componenStateCompute = false;
      } else {
        this.componenStateCompute = true;
      }
      this.componenState = true;
      this.computeMassAttributeList();
    },
    removeSelect(item) {
      if (this.workspaceMode === 'PartDocument') {
        massDistributionCheckCommand.clearEntityOutline(item.id);
      } else if (this.workspaceMode === 'AssemblyDocument'){
        massDistributionCheckCommand.clearHighLightInstances([item.instanceId]);
      }
      if (this.params.massAttributeElements.length > 0) {
        this.computeMassAttribute();
      } else {
        this.massAttributeInfo = null;
      }
    },
    reSet() {
      let instanceId = this.params.massAttributeElements[0].instanceId;
      let massCoverParam = {
        instanceId: instanceId,
      }
      massDistributionCheckCommand.massCoverReSet(massCoverParam);
      this.computeMassAttributeList();
      this.isCovMass = false;
    },
    confirmModal() {
      this.protoRefToInstanceMap.forEach(element => {
        let attItem = this.assemblyTreeMassAttributeInfoList.find(att => att.protoName === element.protoRef.protoRefName);
        element.instanceList.forEach( item => {
          if(!this.isGrouping) {
            attItem = this.assemblyTreeMassAttributeInfoList.find(att => att.protoName === element.protoRef.protoRefName && att.name === item.name);
          }
          if(!(parseFloat(attItem.mass) - attItem.standardMass >= -0.001 && parseFloat(attItem.mass) - attItem.standardMass <= 0.001)) {
            let instanceId = item.instanceId;
            let massCoverValue = attItem.standardMass;
            let massCoverParams = {
              instanceId: instanceId,
              massValue: massCoverValue
            }
            massDistributionCheckCommand.massCoverConfirm(massCoverParams);
          }
        });
      });
      this.computeMassAttributeList();
    },
    computeMassAttributeList() {
      this.massAttributeInfo = null;
      this.assemblyTreeMassAttributeInfoList = [];
      this.massAttribute.no = 0;
      this.loaded = false;
      this.loadedPercent = 20;

      massDistributionCheckCommand.getMassAttributeInfo(this.params.massAttributeElements, (data) => {
        this.assemblyTreeMassAttributeInfoList = [];
	      //总装配
        massDistributionCheckToolManager.parseMassAttributeInfos(data);
	      this.massValue = massDistributionCheckToolManager.massValue;
        this.isCovMass = massDistributionCheckToolManager.isCovMass;
        this.massAttributeInfo = massDistributionCheckToolManager.massAttributeInfo;

        if(this.isAssembly) {
          this.getMassData(this.massAttributeInfo);
          let att = copyJSON(this.massAttribute);
          att.isMassCovered = this.isCovMass;
          att.name = this.assemblyName;
          att.weight = 100.0000;
          att.mass = parseFloat(parseFloat(att.mass) * 1).toFixed(3);
          att.standardWeight = 100.0000;
          att.standardMass = parseFloat(att.mass);
          att.type = 'Assembly';
          this.assemblyMass = parseFloat(att.mass);
          this.assemblyTreeMassAttributeInfoList.push(att);
          this.resultAssemblyTreeMassAttributeInfoList = this.assemblyTreeMassAttributeInfoList;
        }
        //加载百分比
        let countElement = 0;
        let mapLength = this.protoRefToInstanceMap.length;
        if (this.isGrouping) {
          this.loadedPercent = 20 + Math.floor((1 / (mapLength + 1)) * 100 * 0.8);
        } else {
          mapLength = 0;
          this.protoRefToInstanceMap.forEach(item => {
            mapLength +=  item.instanceList.length;
          });
          this.loadedPercent = 20 + Math.floor((1 / (mapLength + 1)) * 100 * 0.8);
        }

        //零件与子装配
        this.protoRefToInstanceMap.forEach(element => {
          let massAttributeElement = [];
          massAttributeElement.push(element.instanceList[0]);
          if (this.isGrouping) {
            massDistributionCheckCommand.getMassAttributeInfo(massAttributeElement, (res) => {
              //Todo,返回结果处理
              massDistributionCheckToolManager.parseMassAttributeInfos(res);
              this.massValue = massDistributionCheckToolManager.massValue;
              this.isCovMass = massDistributionCheckToolManager.isCovMass;
              this.massAttributeInfo = massDistributionCheckToolManager.massAttributeInfo;
              this.getMassData(this.massAttributeInfo);
              let attChild = copyJSON(this.massAttribute);
              attChild.isMassCovered = this.isCovMass;
              attChild.protoName = copyJSON(element.protoRef.protoRefName);
              let name = element.protoRef.protoRefName;
              if(element.protoRef.protoRefDocId !== "") {
                let docIdIndex = name.indexOf(element.protoRef.protoRefDocId);
                if (docIdIndex !== -1) {
                  name = name.substr(0, docIdIndex - 1);
                } else {
                  name = element.instanceList[0].name;
                }
              }
              attChild.name = name;
              attChild.count = element.instanceList.length;
              attChild.mass = parseFloat(parseFloat(attChild.mass) * attChild.count).toFixed(3);
              //权重百分化显示
              attChild.weight = parseFloat((parseFloat(attChild.mass) / this.assemblyMass) * 100).toFixed(4);
              attChild.standardWeight = attChild.weight;
              attChild.standardMass = parseFloat(attChild.mass);
              this.assemblyTreeMassAttributeInfoList.push(attChild);
              countElement++;
              this.loadedPercent = 20 + Math.floor(((countElement + 1) / (mapLength + 1)) * 100 * 0.8);
              if(countElement === mapLength) {
                this.loadedPercent = 100;
                this.resultAssemblyTreeMassAttributeInfoList = this.assemblyTreeMassAttributeInfoList;
                this.loaded = true;
                this.componenState = false;
              }
            });
          } else {
            element.instanceList.forEach(instance => {
              let massInstanceAttributeElement = [];
              massInstanceAttributeElement.push(instance);
              massDistributionCheckCommand.getMassAttributeInfo(massInstanceAttributeElement, (res) => {
                //Todo,返回结果处理
                massDistributionCheckToolManager.parseMassAttributeInfos(res);
                this.massValue = massDistributionCheckToolManager.massValue;
                this.isCovMass = massDistributionCheckToolManager.isCovMass;
                this.massAttributeInfo = massDistributionCheckToolManager.massAttributeInfo;
                this.getMassData(this.massAttributeInfo);
                let attChild = copyJSON(this.massAttribute);
                attChild.isMassCovered = this.isCovMass;
                attChild.protoName = copyJSON(element.protoRef.protoRefName);

                attChild.name = instance.name;
                attChild.count = 1;
                attChild.mass = parseFloat(attChild.mass).toFixed(3);
                //权重百分化显示
                attChild.weight = parseFloat((parseFloat(attChild.mass) / this.assemblyMass) * 100).toFixed(4);
                attChild.standardWeight = attChild.weight;
                attChild.standardMass = parseFloat(attChild.mass);
                this.assemblyTreeMassAttributeInfoList.push(attChild);
                countElement++;
                this.loadedPercent = 20 + Math.floor(((countElement + 1) / (mapLength + 1)) * 100 * 0.8);
                if(countElement === mapLength) {
                  this.loadedPercent = 100;
                  this.resultAssemblyTreeMassAttributeInfoList = this.assemblyTreeMassAttributeInfoList;
                  this.loaded = true;
                  this.componenState = false;
                }
              });
            });
          }
        });
      });
	    this.isComputed = true;
    },
    computePartMassAttributeList() {
      this.massAttributeInfo = null;
      this.assemblyTreeMassAttributeInfoList = [];
      let currentAssemblyTreeMassAttributeInfoList = [];
      this.massAttribute.no = 0;
      this.loaded = false;
      this.loadedPercent = 20;

      massDistributionCheckCommand.getMassAttributeInfo(this.params.massAttributeElements, (data) => {
        currentAssemblyTreeMassAttributeInfoList = [];
        this.assemblyTreeMassAttributeInfoList = [];
        //总装配
        massDistributionCheckToolManager.parseMassAttributeInfos(data);
        this.massValue = massDistributionCheckToolManager.massValue;
        this.isCovMass = massDistributionCheckToolManager.isCovMass;
        this.massAttributeInfo = massDistributionCheckToolManager.massAttributeInfo;

        if(this.isAssembly) {
          this.getMassData(this.massAttributeInfo);
          let att = copyJSON(this.massAttribute);
          att.isMassCovered = this.isCovMass;
          att.name = this.assemblyName;
          att.weight = 100.0000;
          att.mass = parseFloat(parseFloat(att.mass) * 1).toFixed(3);
          att.standardWeight = 100.0000;
          att.standardMass = parseFloat(att.mass);
          att.type = 'Assembly';
          this.assemblyMass = parseFloat(att.mass);
          currentAssemblyTreeMassAttributeInfoList.push(att);
          this.resultAssemblyTreeMassAttributeInfoList = currentAssemblyTreeMassAttributeInfoList;
        }

        //加载百分比
		    let countElement = 0;
        let mapLength = this.protoRefToInstanceMap.length;
        if (this.isGrouping) {
          this.loadedPercent = 20 + Math.floor((1 / (mapLength + 1)) * 100 * 0.8);
        } else {
          mapLength = 0;
          this.protoRefToInstanceMap.forEach(item => {
		        mapLength +=  item.instanceList.length;
          });
          this.loadedPercent = 20 + Math.floor((1 / (mapLength + 1)) * 100 * 0.8);
        }

        //零件与子装配
        this.protoRefToInstanceMap.forEach(element => {
          // if(this.assemblyTreeMassAttributeInfoList.length > 1) {
          //   for(let i = 0; i < this.assemblyTreeMassAttributeInfoList; i++) {
          //     this.assemblyTreeMassAttributeInfoList.pop();
          //   }
          // }
          let massAttributeElement = [];
          massAttributeElement.push(element.instanceList[0]);

          if(this.isGrouping) {
            massDistributionCheckCommand.getMassAttributeInfo(massAttributeElement, (res) => {
              //Todo,返回结果处理
              massDistributionCheckToolManager.parseMassAttributeInfos(res);
              this.massValue = massDistributionCheckToolManager.massValue;
              this.isCovMass = massDistributionCheckToolManager.isCovMass;
              this.massAttributeInfo = massDistributionCheckToolManager.massAttributeInfo;
              this.getMassData(this.massAttributeInfo);
              let attChild = copyJSON(this.massAttribute);
              attChild.isMassCovered = this.isCovMass;
              attChild.protoName = copyJSON(element.protoRef.protoRefName);
              let name = element.protoRef.protoRefName;
              let docIdIndex = name.indexOf(element.protoRef.protoRefDocId);
              if (docIdIndex !== -1) {
                name = name.substr(0, docIdIndex - 1);
              } else {
                name = element.instanceList[0].name;
              }
              attChild.name = name;
              attChild.count = element.instanceList.length;
              attChild.mass = parseFloat(parseFloat(attChild.mass) * attChild.count).toFixed(3);
              //权重百分化显示
              attChild.weight = parseFloat((parseFloat(attChild.mass) / this.assemblyMass) * 100 ).toFixed(4);
              attChild.standardWeight = attChild.weight;
              attChild.standardMass = parseFloat(attChild.mass);
              currentAssemblyTreeMassAttributeInfoList.push(attChild);
              countElement++;
              this.loadedPercent = 20 + Math.floor(((countElement + 1) / (mapLength + 1)) * 100 * 0.8);
              if(countElement === mapLength) {
                this.loadedPercent = 100;
                this.resultAssemblyTreeMassAttributeInfoList = currentAssemblyTreeMassAttributeInfoList;
                this.loaded = true;
                this.componenState = false;
              }
            });
          } else {
            element.instanceList.forEach(instance => {
              let massInstanceAttributeElement = [];
              massInstanceAttributeElement.push(instance);
              massDistributionCheckCommand.getMassAttributeInfo(massInstanceAttributeElement, (res) => {
                //Todo,返回结果处理
                massDistributionCheckToolManager.parseMassAttributeInfos(res);
                this.massValue = massDistributionCheckToolManager.massValue;
                this.isCovMass = massDistributionCheckToolManager.isCovMass;
                this.massAttributeInfo = massDistributionCheckToolManager.massAttributeInfo;
                this.getMassData(this.massAttributeInfo);
                let attChild = copyJSON(this.massAttribute);
                attChild.isMassCovered = this.isCovMass;
                attChild.protoName = copyJSON(element.protoRef.protoRefName);

                attChild.name = instance.name;
                attChild.count = 1;
                attChild.mass = parseFloat(attChild.mass).toFixed(3);
                //权重百分化显示
                attChild.weight = parseFloat((parseFloat(attChild.mass) / this.assemblyMass) * 100) .toFixed(4) ;
                attChild.standardWeight = attChild.weight;
                attChild.standardMass = parseFloat(attChild.mass);
                currentAssemblyTreeMassAttributeInfoList.push(attChild);
                countElement++;
                this.loadedPercent = 20 + Math.floor(((countElement + 1) / (mapLength + 1)) * 100 * 0.8);
                if(countElement === mapLength) {
                  this.loadedPercent = 100;
                  this.resultAssemblyTreeMassAttributeInfoList = currentAssemblyTreeMassAttributeInfoList;
                  this.loaded = true;
                  this.componenState = false;
                }
              });
            });
          }
          this.assemblyTreeMassAttributeInfoList = currentAssemblyTreeMassAttributeInfoList;
        });
      });
      this.isComputed = true;
    },
    exportHistogram() {
      this.isExportHistogram = -this.isExportHistogram;
    },
    computeStandardMass() {
      // let currentMass = parseFloat(this.assemblyTreeMassAttributeInfoList[0].standardMass) === 0 ? this.assemblyTreeMassAttributeInfoList[0].mass : this.assemblyTreeMassAttributeInfoList[0].standardMass;
      let currentMass = parseFloat(this.assemblyTreeMassAttributeInfoList[0].mass);
      let currentStandardWeight = parseFloat(this.assemblyTreeMassAttributeInfoList[0].standardWeight) === 0 ? this.assemblyTreeMassAttributeInfoList[0].weight : this.assemblyTreeMassAttributeInfoList[0].standardWeight;
      let sumStandardWeight = 0;
      this.assemblyTreeMassAttributeInfoList.forEach((item,index) =>{
        sumStandardWeight += parseFloat(item.standardWeight);
      });
      if(sumStandardWeight/2 - currentStandardWeight >= 0.0001) {
        let msg ='零件/子装配标准权重之和大于装配标准权重，请重新输入';
        massDistributionCheckCommand.eventBusAlertMsg(msg, 'error');
        return;
      }
      this.assemblyTreeMassAttributeInfoList.forEach((item,index) =>{
        if(item.weight !== item.standardWeight) {
          item.standardMass = parseFloat(parseFloat(currentMass) * (item.standardWeight / 100)).toFixed(3);
          item.deviationWeight = parseFloat(item.weight - item.standardWeight).toFixed(3);
          item.deviationMass = parseFloat(parseFloat(item.mass) - item.standardMass).toFixed(3);
        }
      });
      let msg ='标准重量计算完成';
      massDistributionCheckCommand.eventBusAlertMsg(msg, 'success');
      this.isMassComputed = -this.isMassComputed;
      this.isMassReComputed = true;
    },
    getMassData(info) {
      ['Density', 'Mass', 'Volume', 'Area', 'BaryCenter', 'T1', 'T2', 'BT', 'OT'].forEach(item => {
        let name = info[item].name;
        let value = info[item].value;
        let unit = info[item].unit;

        if (name === 'Density') {
          // 这里不应该使用科学计数法以后的密度值判断，需根据内核返回的值进行判断
          if(info[item].different) {
            return "各不相同"
          } else {
            this.massAttribute.density = value;
            this.massAttribute.densityUnit = unit;
          }
        } else if (name === "Mass" && !this.isCovMass) {
          this.massAttribute.mass =  massDistributionCheckToolManager.massValue !== '' ? massDistributionCheckToolManager.massValue : value;
          this.massAttribute.massUnit = unit;
        } else if (name === "CovMass" && this.isCovMass) {
          this.massAttribute.mass =  massDistributionCheckToolManager.massValue !== '' ? massDistributionCheckToolManager.massValue : value;
          this.massAttribute.massUnit = unit;
        } else if (name === "Volume") {
          this.massAttribute.volume =  value;
          this.massAttribute.volumeUnit = unit;
        } else if (name === "Area") {
          this.massAttribute.area =  value;
          this.massAttribute.areaUnit = unit;
        } else if (name === "BaryCenter") {
          this.massAttribute.baryCenter.unit =  unit??'mm';
          this.massAttribute.baryCenter.x =  info[item].x.value;
          this.massAttribute.baryCenter.y =  info[item].y.value;
          this.massAttribute.baryCenter.z =  info[item].z.value;
        } else if (name === "T1"){
          this.massAttribute.axesAndMoments.unit = unit??'g * mm²';
          this.massAttribute.axesAndMoments.Lx ='(' + info[item].value.Lx0 + ', ' + info[item].value.Lx1 + ', ' + info[item].value.Lx2 + ')';
          this.massAttribute.axesAndMoments.Ly ='(' + info[item].value.Ly0 + ', ' + info[item].value.Ly1 + ', ' + info[item].value.Ly2 + ')';
          this.massAttribute.axesAndMoments.Lz ='(' + info[item].value.Lz0 + ', ' + info[item].value.Lz1 + ', ' + info[item].value.Lz2 + ')';
        } else if (name === "T2"){
          this.massAttribute.axesAndMoments.unit = unit??'g * mm²';
          this.massAttribute.axesAndMoments.Px =info[item].value.Px;
          this.massAttribute.axesAndMoments.Py =info[item].value.Py ;
          this.massAttribute.axesAndMoments.Pz =info[item].value.Pz;
        } else if (name === "BT"){
          this.massAttribute.BT.unit = unit??'g * mm²';
          this.massAttribute.BT.Lxx =info[item].value.Lxx;
          this.massAttribute.BT.Lyx =info[item].value.Lyx;
          this.massAttribute.BT.Lzx =info[item].value.Lzx;
          this.massAttribute.BT.Lxy =info[item].value.Lxy;
          this.massAttribute.BT.Lyy =info[item].value.Lyy;
          this.massAttribute.BT.Lzy =info[item].value.Lzy;
          this.massAttribute.BT.Lxz =info[item].value.Lxz;
          this.massAttribute.BT.Lyz =info[item].value.Lyz;
          this.massAttribute.BT.Lzz =info[item].value.Lzz;
        } else if (name === "OT"){
          this.massAttribute.OT.unit = unit??'g * mm²';
          this.massAttribute.OT.Lxx =info[item].value.Lxx;
          this.massAttribute.OT.Lyx =info[item].value.Lyx;
          this.massAttribute.OT.Lzx =info[item].value.Lzx;
          this.massAttribute.OT.Lxy =info[item].value.Lxy;
          this.massAttribute.OT.Lyy =info[item].value.Lyy;
          this.massAttribute.OT.Lzy =info[item].value.Lzy;
          this.massAttribute.OT.Lxz =info[item].value.Lxz;
          this.massAttribute.OT.Lyz =info[item].value.Lyz;
          this.massAttribute.OT.Lzz =info[item].value.Lzz;
        }
      });
      this.massAttribute.no += 1;
    },
    changePluginContainerHeight() {
      massDistributionCheckCommand.changePluginContainerHeight({
        minWidth: this.$el.clientWidth, maxHeight: this.$el.clientHeight, width: 1200,
      });
    },
    cancel() {
      for (let i = 0; i < this.params.massAttributeElements.length; i++) {
        if (this.workspaceMode === 'PartDocument') {
          massDistributionCheckCommand.clearEntityOutline(this.params.massAttributeElements[i].id);
        } else if (this.workspaceMode === 'AssemblyDocument'){
          massDistributionCheckCommand.clearHighLightInstances([this.params.massAttributeElements[i].instanceId]);
        }
      }
      if (this.params.massAttributeElements.length > 0) {
        this.params.massAttributeElements = [];
      }
      if (this.massAttributeInfo) {
        this.massAttributeInfo = null;
      }
      massDistributionCheckCommand.clearAllHighLight();
      massDistributionCheckCommand.closeActiveCmd();
    },
    changeMassCover(value) {
      this.isCovMass = value;
      if (!this.isCovMass) {
        this.reSet();
        this.massValue = massDistributionCheckToolManager.massValue;
      } else {
        this.massValue = massDistributionCheckToolManager.massValue;
      }
    },
  },
  watch: {
    "params.massAttributeElements": {
      handler(elements) {
        if (this.workspaceMode === 'PartDocument') {
          let entIds = elements.map(e => e.id);
          if (entIds) {
            massDistributionCheckCommand.highLightEntityOutline(entIds);
          }
        } else if (this.workspaceMode === 'AssemblyDocument') {
          let instanceIds = elements.map(e => e.instanceId);
          if (instanceIds) {
            massDistributionCheckCommand.highLightInstances(instanceIds);
          }
        }
      }
    },
  },
  created() {
    if (!this.isInit){
      this.isComputed = false;
      massDistributionCheckCommand.init(1200, 500, 550, () => {
        massDistributionCheckCommand.clearAllHighLight();
        this.isInit = true;
        if (this.workspaceMode.length === 0) {
          let params = [];
          massDistributionCheckCommand.getWorkspaceMode(params, (data) => {
            this.workspaceMode = data;
            massDistributionCheckCommand.getInstanceTree((instance) => {
              if(instance && instance?.length > 0) {
                this.assemblyTree = instance;
                this.initAssemblyTree();
              } else {
                this.loaded = true;
                this.isEmptyAssemblyTree = true;
              }
            });
          });
        }
        this.changePluginContainerHeight();
      });
    }
  },
  computed:{
    isCompute() {
      return  this.params.massAttributeElements.length === 0 ? false : true;
    },
    isPartDocument() {
      if (this.workspaceMode.length > 0) {
        return this.workspaceMode === 'PartDocument';
      }
    },
    queryListDescription() {
      return this.isPartDocument ? this.$t('message.MassAttributeTool.selectSolids') : this.$t('message.MassAttributeTool.selectInstances');
    },
    queryListTipMsg() {
      return this.isPartDocument ? this.$t('message.MassAttributeTool.TipMsgSolids') : this.$t('message.MassAttributeTool.TipMsgInstances');
    },
    massCover() {
      return (!this.isPartDocument && this.params.massAttributeElements.length === 1 && this.massAttributeInfo) ? true : false;
    },
  },
}
</script>

<style scoped>
.wrap {
    font-size: 12px;
}
.center-text {
    /* 设置居中显示 */
    display: flex;
    justify-content: center;
    align-items: center;
    height: 20px;
    margin: 0;
    padding: 0;
    /* 设置灰色 */
    color: #c6c8ca;
    font-size: 15px;
}
</style>
